//
//  SYBaseViewController.m
//  Foodie
//
//  Created by yunqi on 16/3/10.
//  Copyright © 2016年 SY. All rights reserved.
//

#import "SYBaseViewController.h"
#import "SYNavigationItemBtnView.h"
#import "UIControl+YYAdd.h"
@interface SYBaseViewController ()
{
    
}
PROPERTY_ASSIGN NSInteger viewControllersTotal;
@end

@implementation SYBaseViewController
-(id)init
{
    self=[super init];
    if (self) {
        [self initDefaultData];
    }
    return self;
}
- (instancetype)initWithNibName:(nullable NSString *)nibNameOrNil bundle:(nullable NSBundle *)nibBundleOrNil 
{
    self=[super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        [self initDefaultData];
    }
    return self;
}
- (instancetype)initWithCoder:(NSCoder *)coder
{
    if (self = [super initWithCoder:coder]) {
        [self initDefaultData];
    }
    return self;
}

-(void)initDefaultData
{
    _autoShowBack=YES;
    _canShowGoRoot=YES;
    _thisControllerCanGoRoot=YES;
    _navClearColor=NO;
}

- (void)viewDidLoad {
     [self initNavigationView];
    if (self.navigationController.viewControllers.count>1&&self.autoShowBack) {
            [self SYShowBackButton:YES];
    }
    self.view.backgroundColor=[UIColor whiteColor];
    [super viewDidLoad];
    self.edgesForExtendedLayout =UIRectEdgeAll;
    self.automaticallyAdjustsScrollViewInsets = NO;
      
    // Do any additional setup after loading the view.
}
-(void)setAutoShowBack:(BOOL)autoShowBack
{
    _autoShowBack=autoShowBack;
    [self SYShowBackButton:_autoShowBack];
}
-(void)setNavBarHidden:(BOOL)navBarHidden
{
    _navBarHidden=navBarHidden;
    self.navBarItem.backView.hidden=navBarHidden;
}
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navBarItem.backView.backgroundColor=[UIColor yellowColor] ;
    if (self.navClearColor) {
        self.navBarItem.backView.backgroundColor=[UIColor clearColor];
        self.navBarItem.lineView.backgroundColor=[UIColor clearColor];
    }
    self.navigationController.navigationBarHidden=YES;
    [self.view bringSubviewToFront:self.navBarItem.backView];
    [[UIApplication sharedApplication]setStatusBarStyle:UIStatusBarStyleDefault];
}
-(void)msAutoTextResponder
{
    
}
-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    self.navigationController.navigationBarHidden=YES;
    
}

-(void)BaseControllerClickNavLeftButton:(UIButton *)btn
{
    [self.navigationController popViewControllerAnimated:YES];
}
-(void)BaseControllerClickNavRightButton:(UIButton *)btn
{
    
}
-(void)setTitle:(NSString *)title
{
    [super setTitle:title];
    self.navBarItem.title=title;
}

- (void)initNavigationView
{
    if (!self.navBarItem ) {
        self.navBarItem = [[SYNavigationItem alloc]init];
        self.navBarItem.showController=self;
        self.navBarItem.backView.backgroundColor=[UIColor yellowColor];
    }
}
-(void)SYLeftBtnWithImageName:(NSString *)imageName  title:(NSString *)title
{
    if (!MSStringisEmpty(imageName)||!MSStringisEmpty(title)) {
        
        [self.navBarItem showLeftButtonWithNomoralImage:imageName HighlightedImage:nil title:title];
        [self.navBarItem.navigationLeftBtn.goRootItem removeAllTargets];
        [self.navBarItem.navigationLeftBtn.goBackItem removeAllTargets];
        [self.navBarItem.navigationLeftBtn.goBackItem addTarget:self action:@selector(BaseControllerClickNavLeftButton:) forControlEvents:UIControlEventTouchUpInside];
        [self.navBarItem.navigationLeftBtn.goRootItem addTarget:self action:@selector(baseGotoRootController) forControlEvents:UIControlEventTouchUpInside];
        [self resetShowGotoRoot];
    }else{
        self.navBarItem.centerItemView=nil;
    }
}
-(void)SYRightBtnWithImageName:(NSString *)imageName  title:(NSString *)title
{
    if (!MSStringisEmpty(imageName)||!MSStringisEmpty(title)) {
        [self.navBarItem showRightButtonWithNomoralImage:imageName HighlightedImage:nil title:title];
        [self.navBarItem.navigationRightBtn addTarget:self action:@selector(BaseControllerClickNavRightButton:) forControlEvents:UIControlEventTouchUpInside];
    }else
    {
        self.navBarItem.rigthItemView=nil;
    }
}
-(void)SYShowBackButton:(BOOL)showBack
{
    if (showBack) {
        NSString *leftTitle=nil;
        if (!MSStringisEmpty(self.backTitle)) {
            leftTitle = self.backTitle;
        }
        else if(self.navigationController.viewControllers.count>1){
            leftTitle=((UIViewController *)[self.navigationController.viewControllers objectAtIndex:self.navigationController.viewControllers.count-2]).title;
        }
        if (MSStringisEmpty(leftTitle)) {
            leftTitle=@"返回";
        }
        leftTitle=nil;
//        暂时不用
//        leftTitle=nil;
        [self SYLeftBtnWithImageName:@"backArrowRichTextDetail_blackNew" title:leftTitle];
    }else
    {
        [self SYLeftBtnWithImageName:nil title:nil];
    }
}


-(SYBaseController_TAB)tabType
{
//    FNTabBarController *tabbarController=[[FNAppDelegate shareAppdelegate]appDelegateRootController];
//    if (tabbarController) {
//        if (tabbarController.selectedIndex==0) {
//            return SYBaseController_TAB_HOME;
//        }
//        if (tabbarController.selectedIndex==1) {
//            return SYBaseController_TAB_DYNAMIC;
//        }
//        if (tabbarController.selectedIndex==2) {
//            return SYBaseController_TAB_PUBLISH;
//        }
//        if (tabbarController.selectedIndex==3) {
//            return SYBaseController_TAB_FRIEND;
//        }
//        if (tabbarController.selectedIndex==4) {
//            return SYBaseController_TAB_MY;
//        }
//    }
    return SYBaseController_TAB_UNKNOW;
}

-(NSInteger)viewControllersTotal
{
    return self.navigationController.viewControllers.count;
}
-(void)resetShowGotoRoot
{
    BOOL canShow=YES;
    for (UIViewController *controller in self.navigationController.viewControllers) {
        if ([controller isKindOfClass:[SYBaseViewController class]]) {
            if (((SYBaseViewController *)controller).thisControllerCanGoRoot==NO) {
                canShow=NO;
                break;
            }
        }
    }
    self.navBarItem.navigationLeftBtn.goRootItem.hidden=YES;
    if (self.canShowGoRoot&&canShow) {
        if (self.viewControllersTotal>=3) {
            self.navBarItem.navigationLeftBtn.goRootItem.hidden=NO;
        }
    }
    if (self.navClearColor) {
        [self.navBarItem.navigationLeftBtn.goBackItem setImage:ImageNamed(@"backArrowRichTextDetail_whiteNew") forState:UIControlStateNormal];
        [self.navBarItem.navigationLeftBtn.goRootItem setImage:ImageNamed(@"sy_goRoot_u") forState:UIControlStateNormal];
    }
    [self.navBarItem adjustContent];
}

-(void)baseGotoRootController
{
    [self.navigationController popToRootViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/


@end
